from django.db import models
from django.forms import TextInput, NumberInput, Select
from django.forms.models import ModelForm

from django import forms


# Create your models here.
# 部门
class Department(models.Model):
    name = models.CharField(max_length=100, unique=True)
    address = models.CharField(max_length=100)
    create_date = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'department'

    # 返回部门的名字  下拉选显示的就不再是对象
    def __str__(self):
        return self.name


# 用户   一对多
class User(models.Model):
    username = models.CharField(max_length=100, unique=True, verbose_name='用户名')
    password = models.CharField(max_length=100, verbose_name='密码')
    age = models.IntegerField(verbose_name='年龄')
    gender_choices = (
        (1, '男'),
        (2, '女')
    )
    gender = models.SmallIntegerField(choices=gender_choices, default=1, verbose_name='性别')
    create_date = models.DateTimeField(auto_now_add=True)
    # 外键
    department = models.ForeignKey(Department, on_delete=models.CASCADE, verbose_name='部门')

    class Meta:
        db_table = 'user'


# 模型表单
class UserModelForm(ModelForm):
    class Meta:
        model = User
        # 在表单中渲染所有的属性为表单空间
        # fields = '__all__'
        fields = ['username', 'age', 'gender', 'department']
        # 配置插件  在生成input表单时  动态添加一个class属性 值是 form-control  <input type='text' class = 'form-control'>
        widgets = {
            'username': TextInput(attrs={'class': 'form-control'}),
            'age': NumberInput(attrs={'class': 'form-control'}),
            'gender': Select(attrs={'class': 'form-control'}),
            'department': Select(attrs={'class': 'form-control'}),
        }


# 员工模型
class Emp(models.Model):
    eno = models.CharField(max_length=100, unique=True, verbose_name='工号')
    name = models.CharField(max_length=100, verbose_name='姓名')
    age = models.IntegerField(verbose_name='年龄')
    salary = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='薪资')
    hire_date = models.DateTimeField(verbose_name='入职时间')
    department = models.ForeignKey(Department, on_delete=models.CASCADE, verbose_name='部门')
    create_date = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'emp'


# 自定义一个时间控件
class DateTimeInput(forms.DateTimeInput):
    input_type = 'datetime-local'


# 员工表单模型
class EmpModelForm(ModelForm):
    class Meta:
        model = Emp
        fields = ['eno', 'name', 'age', 'salary', 'hire_date', 'department']
        widgets = {
            'eno': TextInput(attrs={'class': 'form-control'}),
            'name': TextInput(attrs={'class': 'form-control'}),
            'age': NumberInput(attrs={'class': 'form-control'}),
            'salary': NumberInput(attrs={'class': 'form-control'}),
            'hire_date': DateTimeInput(attrs={'class': 'form-control'}),  #  2025-05-16T14:33
            'department': Select(attrs={'class': 'form-control'}),
        }
